package com.iw.adoms.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.iw.adoms.entity.Brand;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import com.iw.adoms.entity.MarchandiseType;
import com.iw.adoms.service.MarchandiseTypeService;
import com.iw.core.orm.PageData;
import com.iw.core.utils.WebUtils;

@Controller
@RequestMapping("/adoms/marchandiseType")
public class MarchandiseTypeAction {
    @Resource
    private MarchandiseTypeService marchandiseTypeService;

    @RequestMapping("/list")
    public ModelMap list(HttpServletRequest request) {
        PageData<MarchandiseType> pageData = new PageData<MarchandiseType>();
        // 给pageData设置参数
        WebUtils.setPageDataParameter(request, pageData);
        pageData = marchandiseTypeService.find(pageData);

        return new ModelMap(pageData);
    }

    @RequestMapping(value = "/new")
    public String addForm(Model model) {
        return "/adoms/marchandiseType/input";
    }

    @RequestMapping(value = "/edit/{id}")
    public String editForm(@PathVariable("id") Integer id, Model model) {
        MarchandiseType marchandiseType = marchandiseTypeService.find(id);
        model.addAttribute(marchandiseType);
        return "/adoms/marchandiseType/input";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(MarchandiseType marchandiseType) {
        if (marchandiseType.isNew()) {
            marchandiseTypeService.save(marchandiseType);
        } else {
            marchandiseTypeService.update(marchandiseType);
        }

        return "redirect:/adoms/marchandiseType/list";// 重定向
    }

    @RequestMapping(value = "/repeat")
    @ResponseBody
    public boolean repeat(String value, Integer id, String property) {
        try {
            List<MarchandiseType> marchandiseTypes = marchandiseTypeService.findList(property, value);
            if (marchandiseTypes != null && marchandiseTypes.size() > 0) {
                if (id != 0) {
                    MarchandiseType m = marchandiseTypeService.find(id);
                    if (property.equals("name") && !m.getName().equals(value)) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        marchandiseTypeService.delete(id);
        return "redirect:/adoms/marchandiseType/list";// 重定向
    }

    @RequestMapping(value = "/visible/{id}")
    public String visible(@PathVariable("id") Integer id) {
        marchandiseTypeService.visible(id);
        return "redirect:/adoms/marchandiseType/list";// 重定向
    }

    @RequestMapping(value = "/unVisible/{id}")
    public String unVisible(@PathVariable("id") Integer id) {
        marchandiseTypeService.unVisible(id);
        return "redirect:/adoms/marchandiseType/list";// 重定向
    }

    @InitBinder
    public void InitBinder(WebDataBinder dataBinder) {
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
    }

}
